<?php


namespace App\Api\Controllers\nongjin;


use App\Models\TfConfig;
use App\Models\TfStore;
use function EasyWeChat\Kernel\Support\get_client_ip;
use Illuminate\Support\Facades\Log;

class PayController extends BaseController
{
    //扫一扫 0-系统错误 1-成功 2-正在支付 3-失败
    public function scan_pay($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $code = $data['code'];
            $total_amount = $data['total_amount'];
            $signkey = $data['signkey'];
            $merchantNo=$data['merchantNo'];
            $terminalNo=$data['terminalNo'];
            $batchNo=$data['batchNo'];
            $post_data = [
                'qrcode' => $code,
                'transAmount' => $total_amount * 100,
                'outTradeNo' => $out_trade_no,
                'traceNo' => '000001',
                'merchantNo'=>$merchantNo,
                'terminalNo'=>$terminalNo,
                'batchNo'=>$batchNo,
                'notifyUrl'=>url('/api/nongjin/pay_notify_url'),
                'payCode'=>$code,
                'nonceStr'=>$this->nonceStr(),
            ];

            $obj = new BaseController();
            $string = $obj->getSignContent($post_data);
            $string = $string . '&key=' . $signkey;
            $sign = strtoupper(md5($string));
            $post_data['sign'] = $sign;
            $url = 'https://epos.ahrcu.com:3443/cposp/pay/microPay';
            $re = $this->http_curl($url, json_encode($post_data));
            $re = json_decode($re, true);
            error_log(date('Y-m-d H:i:s')." 【农商行】： ".json_encode([$re])."\n\n", 3, "/tmp/". 'nsh'.date('Ymd').".log");
            //交易成功
            if ($re['resp_code'] == '999999') {
                return [
                    'status' => 1,
                    'message' => '交易成功',
                    'data' => $re['extra_data'],
                ];
            } //用户输入密码
            elseif ($re['resp_code'] == '700000') {
                return [
                    'status' => 2,
                    'message' => '请用户输入密码',
                    'data' => $re['extra_data'],
                ];

            } else {
                return [
                    'status' => 3,
                    'message' => $re['resp_data'],
                ];
            }


        } catch (\Exception $exception) {
            return ['status' => 0, 'message' => $exception->getMessage(),];
        }
    }


}
